layui.use(['table', 'jquery', 'layer'], function (data) {
    var table = layui.table,
        $ = layui.jquery,
        layer = layui.layer;

    var username = $('input[name="userName"]').val();
    var isLeader = $('input[name="leader"]').val();
    if (username == null || username == '') {
        window.location.href = ctx + "/index"
    }
    var tableIns = table.render({
        elem: "#projectProduceQuality",
        height: 'full-125',
        url: ctx + '/quality/selectByParamsWithProduce',
        page: true,
        cellMinWidth: 80, // 单元格最小宽度
        toolbar: '#toolbarDemo',
        cols: [[// 表头
            {type: 'checkbox'},
            {type: "numbers"},
            {
                title: "项目编号", field: "projectNo", templet: function (data) {
                    return data.projectBean.projectNo
                }
            },
            {
                title: "产品名称", field: "productName", templet: function (data) {
                    return data.projectBean.basicQuotationBean.productName
                }
            },
            {
                title: "产品数量", field: "productCount", templet: function (data) {
                    return data.projectBean.basicQuotationBean.productCount
                }
            },
            {
                title: "交付日期", field: "deliveryDate", templet: function (data) {
                    return data.projectBean.basicQuotationBean.deliveryDate
                }
            }
            ,
            {
                title: "质检类型", field: "qualityType", templet: function (data) {
                    if (data.qualityType == 3) {
                        return "生产质检"
                    }
                }
            },

            {
                title: "状态", field: "qualityStatus", templet: function (data) {

                    if (data.qualityStatus == 0) {
                        return "待指派质检员"
                    }
                    if (data.qualityStatus == 1) {
                        return "质检中"
                    }
                    if (data.qualityStatus == 2) {
                        return "质检完成"
                    }
                }
            },
            {
                title: "质检员", field: "qualityerId", templet: function (data) {

                    if (data.empBean == null) {
                        return "未指派"
                    } else if (data.empBean != null) {
                        return data.empBean.empName
                    }
                }
            },
            {
                title: "操作", templet: function (data) {
                    if (data.qualityStatus == 0 && isLeader == 1) {
                        return '<a  class="layui-btn   layui-btn-xs" lay-event="assign">指派</a>';
                    }
                    if (data.empBean != null) {
                        var empName = data.empBean.empName;
                        if (empName.localeCompare(username) == 0) {
                            return '<a  class="layui-btn layui-btn-primary  layui-btn-xs" lay-event="quality">质检</a>';
                        } else {
                            return '';
                        }
                    } else {
                        return '';
                    }

                }
            }


        ]]

    })

    table.on('toolbar(projectProduceQualityFilter)', function (data) {


        // if (data.event == "assign") {
        //     var checkStatus = table.checkStatus(data.config.id);
        //     var data = checkStatus.data;
        //     openAssignDialog(data);
        // }
    })

    //监听行
    table.on('tool(projectProduceQualityFilter)', function (data) {
        if (data.event == "quality") {
            openQualityDialog(data.data);
        }
        if (data.event == "assign") {
            openAssignDialog(data.data);
        }
    })


    function openQualityDialog(data) {
        if (data.qualityStatus == 2) {
            layer.msg("质检已完成");
            return;
        }
        layer.open({
            type: 2,
            title: "质检---质检结果",
            area: ['700px', '500px'],
            content: ctx + "/quality/toAddProduceQualityResultPage?id=" + data.id,
            shadeClose: true,//点击遮罩关闭层
            maxmin: true
        })

    }

    function openAssignDialog(data) {

        if (data.qualityStatus != 0) {
            layer.msg("已经指派过了", {icon: 6});
            return;
        }
        layer.open({
            // 生产项目指派质检员
            type: 2,
            title: "质检---指派质检员",
            area: ['700px', '500px'],
            content: ctx + "/quality/assignProduceProjectPage?id=" + data.id,
            shadeClose: true,//点击遮罩关闭层
            maxmin: true
        })


    }
})